package com.jeeinc.oa.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientUtils {
	
	public static void main(String[] args) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("client_id", "3693781153");
		param.put("client_secret", "7954135ee119b1fd068b8f41d2de5672grant_type");
		param.put("grant_type", "authorization_code");
		param.put("redirect_uri", "https://api.weibo.com/oauth2/default.html");
		param.put("code", "c58d487f533b371c7db2088acfd9d37b");
		
		String url = "https://api.weibo.com/oauth2/access_token";
		System.out.println(post(url , param));
	
	}
	/**
	 * 
	 * @param url
	 * @param param
	 * @return
	 */
	public static String post(String url, Map<String, Object> param) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		String str = null;
		try {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			for (String key : param.keySet()) {
				nvps.add(new BasicNameValuePair(key, String.valueOf(param.get(key))));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nvps));
			HttpResponse response = httpclient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				str = convertStreamToString(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpPost.abort();
			httpclient.getConnectionManager().shutdown();
		}
		return str;
	}
	
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	public static String get(String url) {
		BufferedReader in = null;
		String content = null;
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			content = convertStreamToString(entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return content;
	}

	private static String convertStreamToString(HttpEntity entity) {
		if (entity == null) {
			return null;
		}
		BufferedReader reader = null;
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			reader = new BufferedReader(new InputStreamReader(entity.getContent()));
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
